using System;
using System.Collections.Generic;
using System.Web;

namespace Wikey.Transformation
{
    public class TransformationPluginManager
    {
        
        private static object lockToken = new object();
        private static TransformationPluginManager Manager; 

        public static TransformationPluginManager Instance
        {
            get
            {
                lock(lockToken)
                {
                    if( Manager == null  )
                    {
                        lock( lockToken )
                        {
                            Manager = new TransformationPluginManager();
                        }
                    }

                    return Manager;
                }
            }    
        }

        public const string TransformerCollectionCacheKey = "Transformers";

        public Dictionary<string,Type> TransformerCollection
        {
            get
            {
                if (HttpContext.Current == null) return new Dictionary<string, Type>();

                if( HttpContext.Current.Cache[TransformerCollectionCacheKey] == null )
                {
                    var pluginLoader = new TransformationPluginLoader().Load();
                    HttpContext.Current.Cache[TransformerCollectionCacheKey] =
                        pluginLoader.TransformerCollectionType;
                }

                return HttpContext.Current.Cache[TransformerCollectionCacheKey] as 
                       Dictionary<string, Type>;
            }
        }
    }
}